﻿/*
	@author : "likaituan",
	@email : "61114579@qq.com",
	@version : "4.0",
	@date : "2010-09-05",
	@lastime : "2011-8-18"
*/


~function(F){
	//LRC歌词类
	F.lyric = F.Class({
		init : function (container, getpos, getlen){
			this.container = F(container);					//歌词外框对象
			var st = this.container.node.style;
			this.container.css("fontSize",st.fontSize||"12px").css("backgroundColor",st.backgroundColor||"#000000").css("color",st.color||"#9a9a9a").css("overflow","hidden").html("");
			this.box = this.container.append("div").css("position","relative");	//创建一个歌词内框
			
			this.hlcolor = this.container.attr("hlcolor") || "#00ff00"; //高亮色
			this.w = this.container.node.offsetWidth;					//歌词外框的宽度
			this.h = Number(this.container.attr("rowheight")||16);		//行高
			this.midPos = Math.round(this.container.node.offsetHeight/2-this.h/2);//歌词外框中心点高度
			this.getpos = getpos;										//获取当前时间
			this.getlen = getlen;										//获取播放结束时间;
			this.hl = [];												//高亮对象
			this.timer = null;											//计时器
			this.data = [];												//歌词数组
			this.row = 0;												//当前行
			this.rows = 0;												//总行数
		},
		//重新定位	
		setPos : function (){
			this.box.anime({top:this.midPos-Math.round(this.row*this.h)});
		},
		//从歌词地址显示
		showFromUrl : function (url){
			F.get(url).callback(F.proxy(this,"showFromString"));
		},
		//从歌词内容显示
		showFromString : function (lyrics){
			if (!/\[\d+:\d+/.test(lyrics)){
				this.box.html("很遗憾，此歌曲暂无匹配的歌词!");
				return;
			}
		
			this.data = this.parseLrc(lyrics, this.container.css("fontSize"));
			this.rows = this.data.length;
			this.box.html("");
			this.hl = [];
			var tmp = null;
			var html=[];
			for (var s,i=0;i<this.rows;i++){
				html[i] = 
				'<div style="position:relative;left:'+(this.w-this.data[i].len)/2+'px;width:'+this.data[i].len+'px;height:'+this.h+'px;">'+
					'<div style="position:absolute;">'+this.data[i].w+'</div>'+
					'<div style="position:absolute;width:0px;white-space:nowrap;overflow:hidden;color:'+this.hlcolor+';">'+this.data[i].w+'</div>'+
				'</div>';
			}
			this.box.html(html.join(""));
			for(var a=this.box.child(),i=0;i<this.rows;i++){
				this.hl[i] = a.item(i).child(1);
			}
			this.row = 0;
			this.setPos();
			this.startShow();
		},
		//启动歌词显示定时器
		startShow : function(){
			window.clearInterval(this.timer);
			if (this.getlen()>0){ //得到总时间之后
				this.data[this.rows-1].t2 = this.getlen();
				this.timer = window.setInterval(this.show.proxy(this), 100);
				return;
			}
			this.timer = window.setInterval(this.startShow.proxy(this), 100);
		},
		//动态显示歌词
		show : function (){
			var t = this.getpos();
			if (t<this.data[0].t1) {
				return; //未开唱退出
			}
			var o = this.data[this.row];		//当前歌词行
			if (t>=o.t1 && t<o.t2){				//如果位于当前行
				this.hl[this.row].width(Math.round((t-o.t1)/(o.t2-o.t1)*o.len));
			}else{					//当行改变并且在最后一行前面时
				this.hl[this.row].width(0);
				if (this.row<this.rows-1 && t>=this.data[this.row+1].t1 && t<this.data[this.row+1].t2){		//如果位于下一行
					this.row++;
				}else{
					this.row = this.seekRow(t);
					document.title = this.row;
					this.setPos();
				}
				//o = this.data[this.row];		//新的当前行歌词
				this.box.anime({top:this.midPos-Math.round(this.row*this.h), duration:this.h*100});
			}
		},
		//二分法检索歌词
		seekRow : function (t){
			var idx;
			var min = 0;
			var max = this.rows-1;
			while (min<=max){
				idx = Math.floor((min+max)/2);
				if (t<this.data[idx].t1){
					max = idx - 1;
				}else if(t==this.data[idx].t2){
					return idx + 1;					
				}else if(t>this.data[idx].t2){
					min = idx + 1;
				}else{
					return idx;
				}
			}
		},
		//分析LRC歌词文本, 存取到数组中, 以供调用
		parseLrc : function (lyrics,fontSize){
			var a = [];
			lyrics = lyrics.replace(/((?:\[\d+:\d+(?:\.\d+)?\])+)([^\[\r\n]*)/g,function(row,labels,word){
				labels.replace(/[^\[\]]+/g,function(label){
					var t = label.split(":");
					a.push({t1:t[0]*60000+t[1]*1000,t2:0,w:word,len:0});
				});
			});
			a.sort(function (row1,row2){return row1.t1-row2.t1});
			var len = a.length;
		
			var offset= /\[offset:(\-?\d+)\]/i.test(lyrics) ? Math.floor(RegExp.$1) : 0;
			//加上时间偏差
			if (offset!=0){
				for (var i=0;i<len;i++){
					a[i].t1 += offset;
				}
			}
			for (var j=0;j<len-1;j++){
				a[j].t2 = a[j+1].t1;
			}
		
			var obj = document.createElement("span");
			document.body.appendChild(obj);
			obj.style.fontSize = fontSize;
			for (var k=0; k<len; k++){
				obj.innerHTML = a[k].w;
				a[k].len = obj.offsetWidth;
			}
			document.body.removeChild(obj);
			obj = null;
		
			var unknown = [0,"未知"];
			a.ar = ( lyrics.match(/\[ar[：:](.+?)\]/i) || unknown ) [1];
			a.al = ( lyrics.match(/\[al[：:](.+?)\]/i) || unknown ) [1];
			a.ti = ( lyrics.match(/\[ti[：:](.+?)\]/i) || unknown ) [1];
			a.by = ( lyrics.match(/\[by[：:](.+?)\]/i) || unknown ) [1];
			a.offset = offset;
			return a;
		}
	});
}($1stjs);